﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Customer.Bussiness_Service;
using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceCore;
using ServiceCore.Tool.Wx.diy_menu;

namespace Customer.Areas.rk_wx.Controllers
{

    [ApiController, Area("wx_server"), Route("/server/[action]")]
    public class wx_serverController : ControllerBase
    {
        private readonly Iwx_service _service;
        private readonly IToole_DI _tool; 
        public wx_serverController(Iwx_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;  
        } 
        [HttpGet]
        public string token_auth(string signature, string timestamp, string nonce, string echostr)
        {
            return _service.server_auth(signature, timestamp, nonce, echostr);
        }

        [HttpPost]
        public string token_auth()
        {
            using (StreamReader streamReader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var postString = streamReader.ReadToEnd();
                try
                {
                    string reply = _service.wx_msg(postString);
                    _tool.di_es.Add_Log($"收到==>{postString}返回{reply}", "wx_service");
                    return reply;
                }
                catch (Exception ex)
                {
                    _tool.di_es.Add_Log($"处理异常==>收到==>{postString}异常{ex.ToString()}", "wx_service");
                    return "success";
                }
            }
        }
        //[HttpGet]
        //public ResultObjModel test_wx(string xml)
        //{
        //    return _service.(xml);
        //}

        #region 微信3X5菜单设置
        [HttpGet]
        public ResultObjModel diy_menu_get()
        { 
            return _service.diy_menu_get();
        }
        [HttpPost]
        public ResultObjModel diy_menu_create(menu_create_params Params)
        {
            return _service.diy_menu_create(Params);
        }
        #endregion
        #region 素材库
        /// <summary>
        /// 获取素材数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_materialcount()
        {
            return _service.get_materialcount();
        }
        /// <summary>
        /// 获取素材库列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel batchget_material(string type, int pagesize = 20, int page = 1)
        {
            return _service.batchget_material(type, pagesize, page);
        }
        /// <summary>
        /// 添加图文消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel add_news([FromBody]List<add_news_articles_item> add_list)
        {
            return _service.add_news(add_list);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel uploadimg(IFormFileCollection files)
        {
            return _service.uploadimg(files);
        }
        /// <summary>
        /// 添加其他素材信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel add_material([FromForm]IFormFileCollection files, [FromForm]string type)
        {
            return _service.add_material(files, type, "", "");
        }
        /// <summary>
        /// 获取单个素材
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_material(string type, string media_id)
        {
            return _service.get_material(type, media_id);
        }
        /// <summary>
        /// 获取单个素材
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultModel del_material(string media_id)
        {
            return _service.del_material(media_id);
        }
        /// <summary>
        /// 更新永久图文消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel update_news(update_news_params model)
        {
            return _service.update_news(model);
        }
        #endregion
        #region 设置微信返回消息
        [HttpPost]
        public ResultObjModel set_follow_return(set_follow_return_params Params)
        { 
            var result = _service.set_follow_return(Params);
            return result;
        } 
        [HttpPost]
        public ResultObjModel set_key_return(set_key_return_params Params)
        {
            var result = _service.set_key_return(Params);
            return result;
        }
        [HttpPost]
        public ResultObjModel set_click_return(set_click_return_params Params)
        {
            var result = _service.set_click_return(Params);
            return result;
        }


        [HttpGet]
        public ResultObjModel set_msg_return_use(string message_id,bool? is_user)
        {
            var result = _service.set_msg_return_use(message_id, is_user);
            return result;
        }
         
        [HttpPost]
        public ResultListModel<wx_message_base> get_wx_msg_pagelist(get_wx_message_pagelist_params Params)
        {
            var result = _service.get_wx_msg_pagelist(Params);
            return result;
        } 
        [HttpGet]
        public ResultObjModel del_wx_msg(string message_id)
        {
            var result = _service.del_wx_msg(message_id);
            return result;
        }
        #endregion
    }
}